/**
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.merak.core.text;

/**
 * Copyright © 2010-2012 Merak Computing. All rights reserved.<br><br>
 *
 * Methods and declarations for StackTextifier.
 *
 * @author fmarinho.
 * @version 1.00.00 - Jul 21, 2012 - fmarinho - initial version.
 *
 * @since MerakCore 1.00.00
 */
public class Wrapper extends Textifier<Object> {

    //~ Attributes /////////////////////////////////////////////////////////////////////////////////
	//**********************************************************************************************
	private final Textifier<Object> textifier;

    //~ Constructors ///////////////////////////////////////////////////////////////////////////////
	//**********************************************************************************************
    @SuppressWarnings("unchecked")
	public Wrapper(Textifier<?> textifier) {

    	this.textifier = (Textifier<Object>) textifier;

	}

	//~ Methods ////////////////////////////////////////////////////////////////////////////////////
	//**********************************************************************************************
	/* (non-Javadoc)
	 * @see org.merak.core.text.Textifier#textify(java.lang.Object, org.merak.core.text.Context)
	 */
	@Override
	public void textify(Object object, Context context) throws Exception {

		// Base Case: Stop when finding a stacked object creating a cycle
		if (context.isStacked(object)) {
			// TODO object replacement by its id, if Identifiable
			context.textify(null);
		}
		/* Recursive Case: render the object, ensuring the rendered objects are
		 * being stacked for recursive verification */
		else {
			context.stack(object);
			this.textifier.textify(object,context);
			context.unstack();
		}

	}
	//**********************************************************************************************

}
